
from copy import deepcopy

import past
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

new_wg_lucky_wheel_api = Blueprint('new_wg_lucky_wheel_api', __name__)
new_wg_lucky_wheel_down_api = Blueprint('new_wg_lucky_wheel_down_api', __name__)

@new_wg_lucky_wheel_api.route('/new_wg_lucky_wheel_action', methods=['GET', 'POST'])
def new_wg_lucky_wheel_action():
  params = json.loads(request.data)
  
 
  _action = params['action'] 
  methods={
    'add_data': addMethod,
    'delete_data': deleteMethod,
    'clone_data': cloneMethod,
    'update_method': updateMethod,
    'update_list_method': updateListMethod,
    'get_item_method': getItemMethod,
    'get_list_method': getListMethod,
  }
  # 获取并执行策略action，执行返回值code为0则表示正常
  action = methods.get(_action, nullMethod)
  result = action(params) 
  
  if result['code'] != 0:
    return {'status': result[code], 'info': result[info]}
  result_data = {'status': 200, 'data': result['data'], 'info': 'request success!'}
  return jsonify(result_data)

@new_wg_lucky_wheel_down_api.route('/new_wg_lucky_wheel_down_action/<int:is_push>', methods=['GET', 'POST'])
def new_wg_lucky_wheel_action(is_push):
  # params = json.loads(request.data)
  params = {
    "is_push": is_push
  }
  
  _action = 'push_issue'
  methods={
    'create_json': createJson,
    'push_issue': pushIssue,
  }
  # 获取并执行策略aciotn
  action = methods.get(_action, nullMethod)
  result = action(params)  
  
  # 直接返回push结果、不用特殊额外的处理
  return result


def nullMethod(param):
  return {'code': -1, 'data': '', 'info': '空方法'}


def addMethod(param):
  return {'code': -1, 'data': '', 'info': '方法未实现'}
  

def deleteMethod(param):
  dbObj = NewWgLuckyWheel.query.filter_by(project=param['project']).first()
  db.session.delete(dbObj)
  db.session.commit()
  projectData = NewWgLuckyWheel.query.filter_by(project=param['project']).first()
  if projectData is not None:
    return {'code': -2, 'data': '', 'info': 'projectData is not None,删除未成功'}
  else:
    return {'code': 0, 'data': '', 'info': '删除成功'}

def cloneMethod(param):
  data = NewWgLuckyWheel.query.filter_by(project=param['project']).first()
  cloneObj = NewWgLuckyWheel(
      remark=data.remark,
      calendar_conf=data.calendar_conf,
      info=data.info,
      limit=data.limit,
      basic=data.basic,
      other=data.other
  )
  print(cloneObj.project)
  db.session.add(cloneObj)
  db.session.commit()

  return {'code': 0, 'data': '', 'info': 'clone success'}


def updateMethod(param):
  project = param['project']
  for name in param['data']:
    item = param['data']
    if name == 'remark':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'remark' : json.dumps(item['remark'])
        })
      db.session.commit()
    if name == 'calendar_conf':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'calendar_conf' : json.dumps(item['calendar_conf'])
        })
      db.session.commit()
    if name == 'info':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'info' : json.dumps(item['info'])
        })
      db.session.commit()
    if name == 'limit':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'limit' : json.dumps(item['limit'])
        })
      db.session.commit()
    if name == 'basic':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'basic' : json.dumps(item['basic'])
        })
      db.session.commit()
    if name == 'other':
      NewWgLuckyWheel.query.filter_by(project=project).update({
        'other' : json.dumps(item['other'])
        })
      db.session.commit()

  return {'code': 0, 'data': '', 'info': 'update success'}

def updateListMethod(param):
  pass

def getItemMethod(param):
  que = NewWgLuckyWheel.query.filter_by(project=param['project']).first()
  if que is None:
    return -2 

  doc_content = GetDocByFilename('wheel_event')
  doc_id_list = []
  for k, v in doc_content.items():
    doc_id_list.append({
      'value': int(k),
      'label': '{0}-{1}'.format(k, v['title'])
    })


  tmp = {
    "project" : int(que.project),
	"remark" : json.loads(que.remark),
	"calendar_conf" : json.loads(que.calendar_conf),
	"info" : json.loads(que.info),
	"limit" : json.loads(que.limit),
	"basic" : json.loads(que.basic),
	"other" : json.loads(que.other),
    "doc_id_list": doc_id_list
  }
  return {'code': 0, 'data': tmp, 'info': 'update success'}

def getListMethod(param):
  data = NewWgLuckyWheel.query.order_by("project").all()
  result = [] 

  for que in data:
    tmp = {
      "project" : int(que.project),
	  "remark" : json.loads(que.remark),
	  "calendar_conf" : json.loads(que.calendar_conf),
	  "info" : json.loads(que.info),
	  "limit" : json.loads(que.limit),
	  "basic" : json.loads(que.basic),
	  "other" : json.loads(que.other),
    }
    result.append(tmp)
  return {'code': 0, 'data': result, 'info': 'update success'}


def createJson(param):
  pass

def pushIssue(param):
  is_push = param['is_push']
  list = NewWgLuckyWheel.query.order_by("project").all()
  projs = {}
  for que in list:
    project = int(que.project)
    remark = json.loads(que.remark)
    calendar_conf = json.loads(que.calendar_conf)
    info = json.loads(que.info)
    limit = json.loads(que.limit)
    basic = json.loads(que.basic)
    other = json.loads(que.other)

    if 'calendar_doc_id' in other:
      _calendar_doc_id = other['calendar_doc_id']
    else:
      _calendar_doc_id = 1

    calendar_conf = {
      'event_type': 50,
      'event_ui': 1,
      'order': 1,
      'jump': 1,
      'localization': {
          'title': _calendar_doc_id,
          'info': _calendar_doc_id
      },
      'reward': info['big_reward'],
      'show_type': 0
    }

    info['rotation']['discount'][0][2] = info['rotation']['discount'][0][2] * 100
    info['rotation']['discount'][1][2] = info['rotation']['discount'][1][2] * 100

    tmp = {
	  "calendar_conf" : calendar_conf,
	  "info" : info,
	  "limit" : limit,
	  "basic" : basic,
    }
    projs[que.project] = tmp
  return DownLoad('lucky_wheel_proj', projs, is_push)


